package com.mingqijia.gassafety.authority.dal.handler;

import com.mingqijia.gassafety.authority.api.req.manager.ChangePasswordReqDTO;
import com.mingqijia.gassafety.authority.api.req.manager.CreateManagerReqDTO;
import com.mingqijia.gassafety.authority.api.req.manager.QueryManagerReqDTO;
import com.mingqijia.gassafety.authority.api.req.manager.UpdateManagerReqDTO;
import com.mingqijia.gassafety.authority.api.resp.manager.ManagerRespDTO;
import com.mingqijia.gassafety.authority.dal.command.*;
import com.mingqijia.gassafety.db.entity.auth.ManagerEntry;
import com.mingqijia.gassafety.shared.utils.MappingTools;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 对象转换接口
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年01月09日 20:37
 */
@Mapper(componentModel = "spring", uses = MappingTools.class)
@ConditionalOnMissingBean(ManagerTranslatorImpl.class)
public interface ManagerTranslator {

    @Mapping(source = "id", target = "userId")
    ManagerRespDTO toRespDTO(ManagerEntry entry);

    ManagerEntry toEntry(CreateManagerCommand command);

    @Mapping(source = "userId", target = "id")
    ManagerEntry toEntry(UpdateManagerCommand command);

    QueryManagerCommand toCommand(QueryManagerReqDTO req);

    CreateManagerCommand toCommand(CreateManagerReqDTO req);

    UpdateManagerCommand toCommand(UpdateManagerReqDTO req);

    ChangePasswordCommand toCommand(ChangePasswordReqDTO req);

    @Mapping(source = "userIds", target = "id")    
    ManagerEntry toEntry(RemoveManagerCommand command);

    default List<ManagerRespDTO> toRespDTO(List<ManagerEntry> entries) {
        return Optional.ofNullable(entries)
                .orElse(Collections.emptyList())
                .stream().map(this::toRespDTO)
                .collect(Collectors.toList());
    }
}
